package com.simon.resourcestatistics.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;

import com.simon.resourcestatistics.dao.IMemberDao;
import com.simon.resourcestatistics.dto.MemberDTO;
import com.simon.resourcestatistics.model.Member;
import com.simon.resourcestatistics.util.CardUtil;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

/**
 * MemberServiceImpl
 */
@Service
public class MemberServiceImpl implements IMemberService {

    @Resource
    private IMemberDao memberDao;

    @Override
    public Page<Member> findPage(MemberDTO dto) {
        Sort sort = Sort.by(Sort.Order.asc("id"));//根据升序排序
        Pageable page = PageRequest.of((dto.getPage() - 1) * dto.getLimit(), dto.getLimit(), sort);
        return memberDao.findAll(getWhereClause(dto), page);
    }

    private Specification<Member> getWhereClause(MemberDTO dto) {
        return new Specification<Member>() {

            @Override
            public Predicate toPredicate(Root<Member> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
               Predicate p=criteriaBuilder.conjunction();
               List<Expression<Boolean>> exList=p.getExpressions();
               if(dto.getCardNo()!=null && !"".equals(dto.getCardNo())){
                   exList.add(criteriaBuilder.equal(root.<String>get("card_no"),dto.getCardNo()));
               }
               if(dto.getName()!=null && !"".equals(dto.getName())){
                   exList.add(criteriaBuilder.like(root.<String>get("name"),"%"+dto.getName()+"%"));
               }
               if(dto.getPhone()!=null && !"".equals(dto.getPhone())){
                   exList.add(criteriaBuilder.equal(root.<String>get("phone"),dto.getPhone()));
               }
               if(dto.getStartDate()!=null){
                exList.add(criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("regdate"), dto.getStartDate()));
               }
               if(dto.getEndDate()!=null){
                   exList.add(criteriaBuilder.lessThanOrEqualTo(root.<Date>get("regdate"), dto.getEndDate()));
               }
               if(dto.getStatus()!=null){
                   exList.add(criteriaBuilder.equal(root.<Integer>get("status"), dto.getStatus()));
               }//判断在线
               if(dto.getCategory()!=null && dto.getCategory()==0){
                   if(dto.getDepartment()!=null && !"".equals(dto.getDepartment())){
                       exList.add(criteriaBuilder.equal(root.<String>get("department"), dto.getDepartment()));
                   }
                   if(dto.getProfession()!=null && !"".equals(dto.getProfession())){
                       exList.add(criteriaBuilder.equal(root.<String>get("profession"), dto.getProfession()));
                   }
                   if(dto.getGrade()!=null && !"".equals(dto.getGrade())){
                       exList.add(criteriaBuilder.equal(root.<String>get("grade"), dto.getGrade()));   
                   }
               }
                return p;
            }
        
        };
    }

    @Override
    public Member saveMember(Member m) {
        // 将会员卡号设置为时间戳
        m.setCardNo(CardUtil.getCardNo());
        return memberDao.save(m);
    }

    @Override
    public void del(Integer id) {
        memberDao.deleteById(id);

    }

    @Override
    public int addAll(List<Member> list) {
       List<Member> memberList=memberDao.saveAll(list);
        return memberList.size();
    }

    @Transactional
    @Override
    public void deleteMember(List<Integer> ids) {
        memberDao.deleteMemberById(ids);
    }


}